/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "oh_device_manager.h"

#include <iostream>

#include "app_manager.h"
#include "dm_client.h"
#include "dm_log.h"
#include "oh_device_manager_err_code.h"

int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int &len)
{
    std::string deviceName = "";
    int32_t ret = OHOS::DistributedHardware::DmClient::GetInstance().GetLocalDeviceName(deviceName);
    if (ret != ERR_OK) {
        LOGE("Get local device name failed, ret=%{public}d", ret);
        return ret;
    }
    len = static_cast<unsigned int>(deviceName.size());
    *localDeviceName = new (std::nothrow) char[len + 1] {0};
    if (*localDeviceName == nullptr) {
        LOGE("create localDeviceName fail");
        return DM_ERR_FAILED;
    }
    if (strcpy_s(*localDeviceName, len + 1, deviceName.c_str()) != EOK) {
        LOGE("copy string fail");
        delete [] *localDeviceName;
        return DM_ERR_FAILED;
    }
    return ERR_OK;
}